/*
 * @Author: lll 1280388921@qq.com
 * @Date: 2025-05-29 14:45:41
 * @LastEditors: lll 1280388921@qq.com
 * @LastEditTime: 2025-05-29 23:36:28
 * @FilePath: /lesson41/udp/maic.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "UdpServer.hpp"
#include <memory>
#include <cstdio>

using namespace std;

void Usage(string proc)
{
    cout << "\n\rUsage: " << proc << " port[1024+]\n"<< endl;
}

string handler(const string& s)
{
    string ret="Server get a msg: "+s;
    cout<<ret<<endl;
    return ret;
}

string ExcuteCommand(const string&cmd)
{
    FILE*fp=popen(cmd.c_str(),"r");
    if(fp==nullptr)
    {
        perror("popen");
        return "error";
    }
    string ret;
    char buffer[4096];
    while(1)
    {
        char*res=fgets(buffer,sizeof(buffer),fp);
        if(res==nullptr)break;
        ret+=res;
    }
    cout<<ret<<endl;
    return ret;
    pclose(fp);
}

// ./udpserver port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    } 
    uint16_t port = stoi(argv[1]);

    unique_ptr<UdpServer> svr(new UdpServer(port));

    svr->Init();
    svr->Run(ExcuteCommand);

    return 0;
}